import{ Observable }from'rxjs/Observable';
import {Injectable} from '@angular/core';
import'rxjs/add/observable/of';

import {User,ModulePermission,ResponseBody,ModuleTree,Permission,Properties} from "../entities/entities"
import {LocalStorage,Http} from "../utils/utils";
import {Router} from "@angular/router";
@Injectable()
export class LoginService{
	/**全部权限*/
	allModulePermissionsString:string="allModulePermissions";
	/**登录用户的权限*/
	modulePermissionsString:string="modulePermissions";
	constructor(private localStorage:LocalStorage,private router:Router,private http:Http,private properties:Properties){
	}
	/**进行登录,并返回此用户的权限*/
	public login(user: User): Promise<void> {
		user.md5Password();
		return this.http.request({
			url:"login",
			data:user
		})/*.then((jsons:any[])=>{
			return jsons.map((e:any)=>{
				return new ModulePermission(
					e["moduleCode"],
					e["moduleName"],
					e["moduleUrl"],
					e["parentCode"],
					e["permissionCode"],
					e["permissionName"],
					e["permissionUrl"]);
			});
		})*/.then((modulePermissions:ModulePermission[])=>{
			this.localStorage.setObject(this.modulePermissionsString,modulePermissions);
		});
	}
	/**返回所有权限,并保存在 localStorage,作为构成左侧菜单栏的超集*/
	public getAllModulePermissionsSaveToLocalStorage():Promise<any>{
		this.localStorage.setObject(this.allModulePermissionsString,null);
		var modulePermissions:ModulePermission[]=this.localStorage.getObject(this.allModulePermissionsString);
		if(!modulePermissions||JSON.stringify(modulePermissions)==="{}"){
			return this.http.request({
				url:"modulePermission/selectAllModulePermissions",
				data:{}
			}).then((modulePermissions:ModulePermission[])=>{
				this.localStorage.setObject(this.allModulePermissionsString,modulePermissions);
			});
		}
	}


	/**判断是否登录*/
	public isLogined():Promise<boolean>{//这个只是判断页面
		return this.http.request({
			url:"isLogined",
			data:{}
		}).then(()=>{
			return true;
		})/*.catch(()=>{
			this.router.navigate(["login"]);
			return false;			
		})*/;
	}

	/**获取节点*/
	public getRootTree():ModuleTree{
		/**所有权限*/
		var allModulePermissions:ModulePermission[]=this.localStorage.getObject(this.allModulePermissionsString);
		allModulePermissions.forEach((e:ModulePermission)=>e.display=false);

		/**登录用户权限*/
		var modulePermissions:ModulePermission[]=this.localStorage.getObject(this.modulePermissionsString);

		/*在 allModulePermissions 中设置 modulePermissions 的权限*/
		allModulePermissions.filter((e:ModulePermission)=>!!modulePermissions.filter((p:ModulePermission)=>p.moduleId===e.moduleId&&p.permissionId===e.permissionId).length).forEach((e:ModulePermission)=>e.display=true);
		this.displayParent(allModulePermissions);

		/*分组*/
		allModulePermissions=allModulePermissions.reduce((a:ModulePermission[],b:ModulePermission)=>{

			var theSameModules= a.filter((e:ModulePermission)=>e.moduleId===b.moduleId);

			if(theSameModules.length===0){
				a.push(b);
			}else if(theSameModules.length===1){
				var theSameModule=theSameModules[0];
				theSameModule.permissions=theSameModule.permissions||[]
				var nodePermissions= theSameModule.permissions;
				if(nodePermissions.length==0){
					nodePermissions.push(new Permission(theSameModule.permissionCode,theSameModule.permissionName,theSameModule.permissionUrl,theSameModule.display));
					theSameModule.permissionCode=null;
					theSameModule.permissionName=null;
					theSameModule.permissionUrl=null;
				}
				nodePermissions.push(new Permission(b.permissionCode,b.permissionName,b.permissionUrl,b.display));
			}
			return a;
		},[]);

		allModulePermissions.filter((e:ModulePermission)=>!e.parentModuleId).forEach((e:ModulePermission)=>e.parentModuleId=this.properties.rootModuleId);

		var rootNode:ModuleTree=new ModuleTree(this.properties.rootModuleId,"rootModule","根模块",null,true);
		this.appendChildren(rootNode,allModulePermissions);
		return rootNode;



	}
	/**设置父级 父级的父级*/
	private displayParent(modulePermissions:ModulePermission[]){
		if(!modulePermissions.reduce((a,b)=>a||b.display,false)){
			return;
		}



		/**可以显示的 module*/
		var displayModulePermissions:ModulePermission[]= modulePermissions.filter((e:ModulePermission)=>e.display);

		/**可以显示的上级 id*/
		var displayParentModuleIds=Array.from(new Set(displayModulePermissions.map((e:ModulePermission)=>e.parentModuleId))).filter((e:number)=>e);
		/** 可以显示的上级*/
		var parentModulePermissions:ModulePermission[]= modulePermissions.filter((e:ModulePermission)=>displayParentModuleIds.includes(e.parentModuleId,0));


		parentModulePermissions.forEach((e:ModulePermission)=>e.display=true);

		var superModulePermissions:ModulePermission[]=modulePermissions.filter((e:ModulePermission)=>!displayModulePermissions.includes(e));
		if(superModulePermissions.length!=0){
			this.displayParent(superModulePermissions);
		}


	}
	private appendChildren(parent:ModuleTree,modulePermissions:ModulePermission[]){

		parent.children=modulePermissions
		.filter((e:ModulePermission)=>parent.moduleId===e.parentModuleId)
		.map((e:ModulePermission)=>{
			if(e.permissions){
				e.display=e.permissions.reduce((a:boolean,b:Permission)=>a||!!b.display,false);
			}
			var moduleTree:ModuleTree=new ModuleTree(e.moduleId,e.moduleCode,e.moduleName,e.moduleUrl,e.display||false);
			moduleTree.permissions=e.permissions;
			return moduleTree;
		});
		parent.children.forEach((e:ModuleTree)=>{
			this.appendChildren(e,modulePermissions);
		});
	}
}
